package ua.edu.nuos.jeetraining2012.cms.entity;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;

/**
 * Students to Quiz normalisation table.
 *
 * @author Victor N. Ganichev victor.ganichev@gmail.com
 * @version 1.0 Date: 23.05.13 Time: 23:48
 * @since 1.0
 */
@Entity
@Table(name = "students_to_quiz")
@IdClass(StudentsToQuizId.class)
public class StudentsToQuizEntity implements Serializable {

    /**
     * Student Id. FK to Students.
     */
    @Id
    @ManyToOne(optional = false)
    @JoinColumn(name = "student_id")
    @NotNull
    private StudentEntity studentId;

    /**
     * Quiz Id. FK to Quiz.
     */
    @Id
    @ManyToOne(optional = false)
    @JoinColumn(name = "quiz_id")
    @NotNull
    private QuizEntity quizId;

    /**
     * Attempt date.
     */
    @Column(name = "attempt_date")
    @Basic(optional = false)
    @Temporal(TemporalType.TIMESTAMP)
    @NotNull
    private Date attemptDate;

    /**
     * Achieved scores.
     */
    @Column(name = "scores")
    @Basic(optional = false)
    @NotNull
    private int stores;

    // Default constructor
    public StudentsToQuizEntity() {}

    // Getters & Setters
    public StudentEntity getStudentId() {
        return studentId;
    }

    public void setStudentId(StudentEntity studentId) {
        this.studentId = studentId;
    }

    public QuizEntity getQuizId() {
        return quizId;
    }

    public void setQuizId(QuizEntity quizId) {
        this.quizId = quizId;
    }

    public Date getAttemptDate() {
        return attemptDate;
    }

    public void setAttemptDate(Date attemptDate) {
        this.attemptDate = attemptDate;
    }

    public int getStores() {
        return stores;
    }

    public void setStores(int stores) {
        this.stores = stores;
    }

    // Overrides
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        StudentsToQuizEntity that = (StudentsToQuizEntity) o;

        if (quizId != null ? !quizId.equals(that.quizId) : that.quizId != null) return false;
        if (studentId != null ? !studentId.equals(that.studentId) : that.studentId != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = studentId != null ? studentId.hashCode() : 0;
        result = 31 * result + (quizId != null ? quizId.hashCode() : 0);
        return result;
    }
}
